﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using DAL;
using BLL;

namespace CuaHangBangDia.QLKHThue
{
    public partial class KhachHangThueForm : Form
    {
        public KhachHangThueForm()
        {
            InitializeComponent();

        }
        private int item = 20;
        private int tong;
        private int page = 1;
        public static int TongTrang(int soluongdl, int num)
        {
            int kq = (int)(soluongdl / num);
            if (kq * num == soluongdl)
                return kq;
            return kq + 1;
        }
        private void LoadKH(List<KhachHangThue> kh)
        {
            dgvKHThue.DataSource = kh;
            tong = TongTrang(KhachHangThue.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;

        }
        public List<KhachHangThue> getPage(int page)
        {
            var query = from c in KhachHangThue.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            gbPhanTrang.Visible = true;
            LoadKH(getPage(1));
            txtTrang.Text = Convert.ToString(1);
            page = 1;
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            ChiTietKHTHueForm frm = new ChiTietKHTHueForm();
            for (int i = 0; i < dgvKHThue.Rows.Count; i++)
            {
                if (dgvKHThue.Rows[i].Selected || dgvKHThue.Rows[i].Cells[0].Value != null)
                {
                    int ma = Convert.ToInt32(dgvKHThue.Rows[i].Cells[1].Value);
                    frm.dgvChiTietKHThue.DataSource = ChiTietKhachHangThue.Objects.Find(k => k.MaKhachHang == ma).ToList();
                }

            }
            frm.ShowDialog();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThemKhachHangThueForm frm = new ThemKhachHangThueForm();
            frm.ShowDialog();
            dgvKHThue.DataSource = KhachHangThue.Objects.GetAll().ToList();
        }
        private List<KhachHangThue> LayKH()
        {
            List<KhachHangThue> lists = new List<KhachHangThue>();
            if (dgvKHThue.Rows.Count > 0)
            {
                for (int i = 0; i < dgvKHThue.Rows.Count; i++)
                {
                    try
                    {
                        if (dgvKHThue.Rows[i].Cells[0].Value != null)
                        {
                            KhachHangThue kh = GetDataKH(i);
                            lists.Add(kh);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            return lists;
        }
        private KhachHangThue GetDataKH(int i)
        {
            KhachHangThue kh = new KhachHangThue();
            kh.MaKhachHang = Convert.ToInt32(dgvKHThue.Rows[i].Cells[1].Value.ToString());
            kh.TenKhachHang = dgvKHThue.Rows[i].Cells[2].Value.ToString();
            kh.Tuoi = Convert.ToInt32(dgvKHThue.Rows[i].Cells[3].Value.ToString());
            kh.DiaChi = dgvKHThue.Rows[i].Cells[4].Value.ToString();
            kh.NgayThue = Convert.ToDateTime(dgvKHThue.Rows[i].Cells[5].Value.ToString());
            return kh;
        }


        private void XoaCTKHThue()
        {
            for (int i = 0; i < dgvKHThue.Rows.Count; i++)
            {
                int ma = Convert.ToInt32(dgvKHThue.Rows[i].Cells[1].Value);
                List<ChiTietKhachHangThue> f = ChiTietKhachHangThue.Objects.Find(k => k.MaKhachHang == ma).ToList();
                foreach (ChiTietKhachHangThue item in f)
                {
                    ChiTietKhachHangThue.Objects.Delete(item);
                }

            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool kq = false;
                List<KhachHangThue> list = LayKH();
                if (list.Count > 0)
                {
                    DialogResult que = MessageBox.Show("Bạn chắc chắn muốn Xóa " + list.Count + " khách hàng thuê?", "Hỏi",
                     MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (que == DialogResult.OK)
                    {
                        XoaCTKHThue();
                        foreach (KhachHangThue item in list)
                        {
                            kq = KhachHangThue.Objects.Delete(item);
                        }
                        if (kq)
                        {
                            MessageBox.Show("Xóa khách hàng thành công !", "Thông Tin",
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
                            btnLoad.PerformClick();
                        }
                    }
                    else
                    {
                        for (int i = 0; i < dgvKHThue.Rows.Count; i++)
                        {
                            dgvKHThue.Rows[i].Cells[0].Value = false;
                        }
                    }
                }
                else
                    MessageBox.Show("Vui lòng chọn Khách Hàng  muốn xóa", "Thông Tin",
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception)
            {
                MessageBox.Show("Xóa Lỗi", "Lỗi",
                             MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private void dgvKHThue_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            bool _selectAll = false;
            int n = dgvKHThue.Rows.Count;
            if (e.ColumnIndex == 0 && n > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < n; idx++)
                {
                    dgvKHThue.Rows[idx].Selected = _selectAll;
                    dgvKHThue.Rows[idx].Cells[0].Value = _selectAll;
                }
                dgvKHThue.Rows[n - 1].Selected = false;
                dgvKHThue.Rows[n - 1].Cells[0].Value = false;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                bool kq = false;
                List<KhachHangThue> list = LayKH();
                if (list.Count > 0)
                {
                    DialogResult que = MessageBox.Show("Bạn chắc chắn muốn Cập Nhật lại thông tin khách hàng thuê đã chọn?", "Hỏi",
                           MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (que == DialogResult.OK)
                    {
                        foreach (KhachHangThue item in list)
                        {
                            kq = KhachHangThue.Objects.Update(item);
                        }
                        if (kq)
                        {
                            MessageBox.Show("Cập Nhật Thành Công", "Thông Tin",
                           MessageBoxButtons.OK, MessageBoxIcon.Information);
                            btnLoad.PerformClick();
                        }
                        else
                            MessageBox.Show("Cập Nhật Lỗi", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        btnLoad.PerformClick();

                    }
                }
                else
                    MessageBox.Show("Vui lòng chọn Khách Hàng  muốn sửa", "Thông Tin",
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception)
            {
                MessageBox.Show("Lỗi cập nhật", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void TimKiemTheoNgayThue(DateTime t)
        {
            dgvKHThue.DataSource = KhachHangThue.Objects.Find(tkkh => (tkkh.NgayThue.Day == t.Day && tkkh.NgayThue.Month == t.Month &&
                tkkh.NgayThue.Year == t.Year)).ToList();

        }
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                string info = txtTimKiem.Text;
                if (info != "")
                {
                    List<KhachHangThue> listten = KhachHangThue.Objects.Find(ten => ten.TenKhachHang.Contains(info)).ToList();
                    List<KhachHangThue> listdc = KhachHangThue.Objects.Find(dc => dc.DiaChi.Contains(info)).ToList();
                    if (listten.Count > 0 || listdc.Count > 0)
                    {
                        if (listten.Count > 0)
                        {
                            LoadKH(listten);
                            gbPhanTrang.Visible = true;
                            LoadKH(getPage(1));
                            txtTrang.Text = Convert.ToString(1);
                            page = 1;
                        }
                        if (listdc.Count > 0)
                        {
                            LoadKH(listdc);
                            gbPhanTrang.Visible = true;
                            LoadKH(getPage(1));
                            txtTrang.Text = Convert.ToString(1);
                            page = 1;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Không tìm thấy Khách hàng có thông tin là : " + info, "Thông Báo",MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                        txtTimKiem.Text = "";
                    }
                }
                else
                    MessageBox.Show("Chưa nhập thông tin", "Thông Báo", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }
            catch (System.Exception)
            {
                MessageBox.Show("Lỗi Tìm kiếm", "Lỗi", MessageBoxButtons.OK,
                           MessageBoxIcon.Error);
            }
            
        }

        private void KhachHangThueForm_Load(object sender, EventArgs e)
        {
            gbPhanTrang.Visible = false;
        }

        private void btnTDau_Click(object sender, EventArgs e)
        {
            if (page > 1)
            {
                LoadKH(getPage(1));
                txtTrang.Text = Convert.ToString(1);
                page = 1;
            }
        }

        private void btnLui_Click(object sender, EventArgs e)
        {
            if (page >= 2)
            {
                page--;
                txtTrang.Text = Convert.ToString(page);
                LoadKH(getPage(page));
            }
        }

        private void btnTien_Click(object sender, EventArgs e)
        {
            if (page < tong)
                page++;
            if (page <= tong)
            {
                txtTrang.Text = Convert.ToString(page);
                LoadKH(getPage(page));
            }
        }

        private void btnTCuoi_Click(object sender, EventArgs e)
        {

            if (page < tong)
            {
                LoadKH(getPage(tong));
                txtTrang.Text = Convert.ToString(tong);
                page = tong;
            }
        }

        private void btnTimTheoNgay_Click(object sender, EventArgs e)
        {
            TimKiemTheoNgayThue(dtpTheoNgay.Value);
        }

        private void txtTrang_KeyDown(object sender, KeyEventArgs e)
        {
            int t;
            if (e.KeyData == Keys.Enter)
            {
                try
                {
                    t = Convert.ToInt32(txtTrang.Text);
                    if (t <= tong && t > 0)
                    {
                        LoadKH(getPage(t));
                        txtTrang.Text = Convert.ToString(t);
                        page = t;
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nhâp Trang Muốn Đến");
                }

            }
        }

        private void dgvKHThue_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            List<KhachHangThue> list = new List<KhachHangThue>();
            string ten = txtTimKiem.Text;
            {
                list = KhachHangThue.Objects.Find(b => b.TenKhachHang.Contains(ten)).ToList();
                if (list.Count > 0)
                {
                    LoadKH(list);
                    gbPhanTrang.Visible = false;
                }
            }            
        }

        private void txtTimKiem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().IndexOfAny(@"0123456789!@#$%^&*()_+=|\{}[]?><.,';:".ToCharArray()) != -1)
            {
                e.Handled = true;
            }

            else
            {
                e.Handled = false;
            }
        }

        

        private void txtTrang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString().IndexOfAny(@"qwertyuioplkjhgfdsazxcvbnm!@#$%^&*()_+=|\{}[]?><.,';:".ToCharArray()) != -1)
            {
                e.Handled = true;
            }

            else
            {
                e.Handled = false;
            }
        }

        private void dgvKHThue_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dgvKHThue_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            
        }
         
       






    }
}
